set scheme s2color

use "$project_path/data/4_regdata/regdata", clear

local subsample_list = "all asia_easteur africa latin_america west_oceania pres_regime parl_regime democ autoc pres parl parlmaj parlprop pre1990 post1990 incballot not_incballot"
local colnames = `""Full sample" "Asia and Eastern Europe" "Africa" "Latin America and Caribbean" "West. Eur., North Am., and Oceania" "Presidential systems" "Parliamentary systems" "Democracies" "Autocracies" "Presidential elections" "Parliamentary elections" "Parl. elec., majoritarian systems" "Parl. elec., proportional systems" "Pre-1990" "Post-1990" "Incumbent on ballot" "Incumbent not on ballot""'
local nb_samples = length("`subsample_list'") - length(subinstr("`subsample_list'", " ", "", .)) + 1

foreach v in diff_m1_chix_econ std_diff_m1_hdi std_diff_m1_democracy_index diff_m1_general_chix {

	if "`v'"=="diff_m1_chix_econ" local v_short = "econ"
	if "`v'"=="std_diff_m1_hdi" local v_short = "hdi"
	if "`v'"=="std_diff_m1_democracy_index" local v_short = "democ"
	if "`v'"=="diff_m1_general_chix" local v_short = "general"
	
	matrix res_mat_`v_short' = J(`nb_samples',3,.)
	matrix coln res_mat_`v_short' = tau ci_low ci_high 
	matrix rown res_mat_`v_short' = `colnames'

	local row = 0

	foreach subsample in `subsample_list' {
		dis "`v', `subsample'"
		local row = `row'+1
		capture rdrobust `v' runvar if subsample_`subsample'==1, level(90)
		if _rc==0 {
			matrix res_mat_`v_short'[`row',1] = e(tau_cl)
			matrix res_mat_`v_short'[`row',2] = e(ci_l_rb)
			matrix res_mat_`v_short'[`row',3] = e(ci_r_rb)
		}
		if _rc!=0 {
			matrix res_mat_`v_short'[`row',1] = .
			matrix res_mat_`v_short'[`row',2] = .
			matrix res_mat_`v_short'[`row',3] = .
		}
	}
		
}

coefplot ///
(matrix(res_mat_general[.,1]), ci((res_mat_general[.,2] res_mat_general[.,3])) offset(+0.3) color(midblue) ciopts(lwidth(*2) lcolor(midblue) )) ///
(matrix(res_mat_econ[.,1]), ci((res_mat_econ[.,2] res_mat_econ[.,3])) offset(+0.1) color(dkgreen) msymbol(O) mfcolor(white) ciopts(lwidth(*2) lcolor(dkgreen) )) ///
(matrix(res_mat_hdi[.,1]), ci((res_mat_hdi[.,2] res_mat_hdi[.,3])) offset(-0.1) color(midgreen) msymbol(O) mfcolor(white) ciopts(lwidth(*2) lcolor(midgreen) )) ///
(matrix(res_mat_democ[.,1]), ci((res_mat_democ[.,2] res_mat_democ[.,3])) offset(-0.3) color(sand) msymbol(O) mfcolor(white) ciopts(lwidth(*2) lcolor(sand) )) ///
, xline(0) xlabel(, labsize(small)) ylabel(, labsize(small)) graphregion(color(white)) xtitle("Standard deviations", size(small)) legend(label(2 "General index") label(4 "Economic performance") label(6 "HDI") label(8 "Democracy") rows(4) region(col(white)) size(small)) xsize(5) ysize(6)
graph export "$project_path/output/figures/Figure_5.pdf", as(pdf) name("Graph") replace

